package com.example.administrator.readbook.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.administrator.readbook.R;
import com.example.administrator.readbook.adapter.AdapterChapterList;
import com.example.administrator.readbook.bean.ChapterListBean;
import com.example.administrator.readbook.greendao.BookDao;
import com.example.administrator.readbook.greendao.GreenDaoReadChapterBean;
import com.example.administrator.readbook.greendao.ReadChapterDao;
import com.example.administrator.readbook.http.ServiceFactory;
import com.example.administrator.readbook.http.ServiceUrl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.example.administrator.readbook.http.ServiceFactory.schedulersTransformer;

public class ChapterListActivity extends AppCompatActivity {
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView chapterListView;
    private ImageView sortImg;
    private List<ChapterListBean.ChaptersBean> chapterList = new ArrayList<>();
    private AdapterChapterList adapterChapterList;
    private String bookId, bookName;
    private boolean downSort = true;
    private GreenDaoReadChapterBean readChapterBean;

    @Override
    protected void onResume() {
        super.onResume();
        readChapterBean = ReadChapterDao.queryReadChapter(bookName);
        adapterChapterList.setReadChapter(readChapterBean != null ? readChapterBean.getReadChapterBookReadChapter() : "0");
        getChapterListData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter_list);
        bookId = getIntent().getStringExtra("bookId");
        bookName = getIntent().getStringExtra("bookName");
        readChapterBean = ReadChapterDao.queryReadChapter(bookName);
        sortImg = findViewById(R.id.chapter_sort);
        swipeRefreshLayout = findViewById(R.id.swiperefreshlayout);
        chapterListView = findViewById(R.id.chapter_listview);
        adapterChapterList = new AdapterChapterList(this, chapterList,
                readChapterBean != null ? readChapterBean.getReadChapterBookReadChapter() : "0");
        chapterListView.setAdapter(adapterChapterList);
        chapterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ReadChapterDao.insertReadChapter(new GreenDaoReadChapterBean(bookName, bookId, bookName, chapterList.get(position).getTitle(), String.valueOf(position)));
                Intent intent = new Intent(ChapterListActivity.this, ChapterDetailActivity.class);
                intent.putExtra("bookLink", chapterList.get(position).getLink());
                startActivity(intent);
            }
        });
        getChapterListData();
        sortImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (downSort) {
                    sortImg.setImageResource(R.mipmap.up);
                    downSort = false;
                    Collections.reverse(chapterList);
                    adapterChapterList.notifyDataSetChanged();
                } else {
                    sortImg.setImageResource(R.mipmap.down);
                    downSort = true;
                    Collections.reverse(chapterList);
                    adapterChapterList.notifyDataSetChanged();
                }
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getChapterListData();
            }
        });
    }

    public void getChapterListData() {
        ServiceUrl searchService = ServiceFactory.getServiceBook();
        HashMap<String, String> params = new HashMap<>();
        params.put("view", "chapters");
        searchService.getChapterListData(bookId, params).compose(schedulersTransformer).subscribe(new Observer<ChapterListBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ChapterListBean chapterListBean) {
                chapterList.clear();
                chapterList.addAll(chapterListBean.getChapters());
                adapterChapterList.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
                if (readChapterBean != null) {
                    chapterListView.setSelection(Integer.parseInt(readChapterBean.getReadChapterPosition() != null ? readChapterBean.getReadChapterPosition() : "0"));
                } else {
                    chapterListView.setSelection(0);
                }
                BookDao.updateBook(bookName, chapterListBean.getChapters().get(chapterListBean.getChapters().size() - 1).getTitle());
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
